<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1">
    <!-- Google Chrome Frame也可以让IE用上Chrome的引擎: -->
    <meta name="renderer" content="webkit">
    <!--国产浏览器高速模式-->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>作业管理系统</title>

    <!-- 公共样式 开始 -->
    <link rel="stylesheet" type="text/css" href="../css/base.css">
    <link rel="stylesheet" type="text/css" href="../css/iconfont.css">
    <script type="text/javascript" src="../framework/jquery-1.11.3.min.js"></script>
    <link rel="stylesheet" type="text/css" href="../layui/css/layui.css">
    <script type="text/javascript" src="../layui/layui.js"></script>

    <!-- 滚动条插件 -->
    <link rel="stylesheet" type="text/css" href="../css/jquery.mCustomScrollbar.css">
    <script src="../framework/jquery-ui-1.10.4.min.js"></script>
    <script src="../framework/jquery.mousewheel.min.js"></script>
    <script src="../framework/jquery.mCustomScrollbar.min.js"></script>
    <script src="../framework/cframe.js"></script><!-- 仅供所有子页面使用 -->
    <!-- 公共样式 结束 -->
    <script type="text/javascript" src="../framework/frameAll.js"></script>
</head>

<body>
<div class="cBody">
    <form id="addForm" class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">原始密码</label>
            <div class="layui-input-inline shortInput">
                <input type="password" name="password" required lay-verify="password" autocomplete="off"
                       placeholder="密码为6到12位，且不能出现空格" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-inline shortInput">
                <input type="password" name="newPassword" required lay-verify="newPassword" autocomplete="off"
                       placeholder="新密码与原始密码不得一致" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认新密码</label>
            <div class="layui-input-inline shortInput">
                <input type="password" name="rePassword" required lay-verify="rePassword" autocomplete="off"
                       placeholder="重复密码与新密码必须一致" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-radius" lay-submit lay-filter="submitBut">修&nbsp;&nbsp;&nbsp;&nbsp;改</button>
                <button type="reset" class="layui-btn layui-btn-radius layui-btn-primary">重&nbsp;&nbsp;&nbsp;&nbsp;置</button>
            </div>
        </div>
    </form>

    <script>
        layui.use('form', function () {
            var form = layui.form;
            //监听提交
            form.on('submit(submitBut)', function (data) {
                var field = data.field;
                $.ajax({
                    type: "post",
                    url: "/UserServlet.do",
                    data: {
                        action: "updatePassword",
                        password: field.password,
                        newPassword: field.newPassword
                    },
                    dataType: "text",
                    success: function (data) {
                        if (parseInt(data) == 0) {
                            top.location.href = "/login.html";
                        } else {
                            layer.msg("原始密码错误！", {
                                offset: '100px',
                                icon: 5,
                                shift: 6
                            });
                            $('input[name=password]').focus();
                        }
                    }
                });
                return false;
            });
            form.verify({
                password: [/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'],
                newPassword: function (value) {
                    if (!/^[\S]{6,12}$/.test(value)) {
                        return '密码必须6到12位，且不能出现空格！';
                    } else if ($('input[name=password]').val() === value) {
                        return '新密码与原始密码相同！';
                    }
                },
                rePassword: function (value) {
                    if ($('input[name=newPassword]').val() !== value)
                        return '两次密码输入不一致！';
                }
            });
        });
    </script>

</div>
</body>

</html>